package com.bac.bacplatform;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.Bill;
import com.bac.utils.BillListAdapter;
import com.bac.utils.Config;
import com.bac.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@SuppressWarnings("deprecation")
public class BillActivity extends BaseActivity {
	private ListView bill_listview;
	private List<Bill> mBillList;
	private BillListAdapter bla;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill);
		findViewById(R.id.title_left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
//		testInformation();
		mBillList = new ArrayList<Bill>();
		getBill();
	}

	@Override
	public void init() {
		bill_listview = (ListView) findViewById(R.id.bill_listview);
	}

	/**
	 * 获取当前用户账单
	 */
	public void getBill() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {

				dismissLoadingProgressNow();
				Log.i("TAG----success", response.toString()); 

				if (response.length() > 0) {
					// 服务器有数据时Gson解析
					Gson gson = new Gson();
					try {
						mBillList = gson.fromJson(response, new TypeToken<List<Bill>>() {}.getType());
						Log.i(getClass().getName()+"bill size----", ""+mBillList.size());
						for(int i = 0; i< mBillList.size(); i++){
							if (!mBillList.get(i).getStatus().equals("交易成功")) {
								Log.i("bad bill---", mBillList.get(i).getStatus()+" "+mBillList.get(i).getTransactionTime());
								mBillList.remove(i);
							}
						}
						Log.i(getClass().getName()+"new bill size----", ""+mBillList.size());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				

				bla = new BillListAdapter(BillActivity.this, mBillList);
				bill_listview.setAdapter(bla);
			}
		};

		showLoadingProgress();
		String path = Config.URL_GAS_BILL;
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "0");
		map.put("size", "10");
		Util.httpRequestToGetWithParams(path, sL, this, map);
	}
	
//	private void testInformation() {
//		lb = new ArrayList<Bill>();
//		Bill b1 = new Bill();
//		b1.setAmount(new BigDecimal(100));
//		b1.setStatus("交易成功");
//		b1.setCode("揩油宝-转入");
//		b1.setTransactionTime(Util.format.format(new Date(2015, 1, 20, 20, 30, 32)));
//		Bill b2 = new Bill();
//		b2.setAmount(new BigDecimal(100));
//		b2.setStatus("交易成功");
//		b2.setCode("揩油宝-转入");
//		b2.setTransactionTime(Util.format.format(new Date(2015, 2, 21, 20, 30, 38)));
//		Bill b3 = new Bill();
//		b3.setAmount(new BigDecimal(100));
//		b3.setStatus("交易成功");
//		b3.setCode("揩油宝-转入");
//		b3.setTransactionTime(Util.format.format(new Date(2015, 2, 22, 20, 30, 37)));
//		Bill b4 = new Bill();
//		b4.setAmount(new BigDecimal(100));
//		b4.setStatus("交易成功");
//		b4.setCode("揩油宝-转入");
//		b4.setTransactionTime(Util.format.format(new Date(2015, 3, 23, 20, 30, 36)));
//		Bill b5 = new Bill();
//		b5.setAmount(new BigDecimal(100));
//		b5.setStatus("交易成功");
//		b5.setCode("揩油宝-转入");
//		b5.setTransactionTime(Util.format.format(new Date(2015, 3, 24, 20, 30, 35)));
//		lb.add(b1);
//		lb.add(b2);
//		lb.add(b3);
//		lb.add(b4);
//		lb.add(b5);
//	}
}
